package com.jucai.pppeiyouclient.utils;

import android.content.Context;
import android.os.Build;
import android.text.InputType;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by chenjidong on 2016-01-31.
 */
public class CommonUtil {
    /**
     * 弹出键盘
     *
     * @param editText
     */
    public static void showSoftInput(final EditText editText) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) editText
                        .getContext().getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(editText, 0);
            }
        }, 200);
    }

    /**
     * 隐藏键盘
     */
    public static void hideSoftInput(final EditText editText) {
        InputMethodManager imm = (InputMethodManager) editText
                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//		imm.showSoftInput(editText,InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    /**
     * 禁止Edittext弹出软件盘，光标依然正常显示。
     */
    public static void disableShowSoftInput(EditText editText) {
        if (Build.VERSION.SDK_INT <= 10) {
            editText.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method method;
            try {
                method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(editText, false);
            } catch (Exception e) {
                // TODO: handle exception
            }

            try {
                method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(editText, false);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

    public static double getAlpha(int scrollY, int maxPoint) {
        int max = 255;
        if (scrollY * 0.5 > max)
            return maxPoint;
        else if (scrollY * 0.5 < 10)
            return 0;
        else {
            double alpha = 0;
            alpha = maxPoint / max * scrollY * 0.5;
            return alpha;
        }
    }

    public static boolean isExistSDCard() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }

    public static String getFrescoCachePath(Context context) {
        /*String path;
        if (CommonUtil.isExistSDCard()) {//android 6.0 以后需要申请权限才能使用sd卡
            path = Environment.getExternalStorageDirectory().getPath();
        } else {
            path = context.getApplicationContext().getCacheDir().getPath();
        }*/
        return context.getApplicationContext().getCacheDir().getPath();
    }
}
